import java.util.*;

/**
 * 49. 字母异位词分组
 * https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked
 */
class Solution3 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for(String str : strs) {
            char[] ch = str.toCharArray();
            Arrays.sort(ch);
            String tmp = ch.toString();
            List<String> list = map.getOrDefault(tmp, new ArrayList<>());
            list.add(str);
            map.put(tmp, list);
        }
        List<List<String>> ret = new ArrayList<>();
        for(String x : map.keySet()) {
            ret.add(map.get(x));
        }
        return ret;
    }
}